package ru.ipccenter.jshell.backend.server;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpSession;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;



@ServerEndpoint(value = "/std-in-out-endpoint", configurator = StdInOutConfigurator.class)
public class StdInOutEndpoint {

  private static final Logger LOG = Logger.getLogger(StdInOutEndpoint.class.getName());
  private HttpSession httpSession;
  
  
  public void setHttpSession(HttpSession session) {
    
    LOG.info("setHttpSession: " + session.getId());
    httpSession = session;
  }
  
  @OnMessage
  public void message(String message, Session session) throws IOException {
    
    LOG.info("New message (session: " + httpSession.getId() + ", message: " + message + ")");
    session.getBasicRemote().sendText("Hello! " + message);
  }
  
  @OnOpen
  public void open(Session session) {

    LOG.info("WS open (session: " + httpSession.getId() + ")");
  }
}
